package com.boxing;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

@WebServlet(name = "DownLoadServlet", value = "/DownLoadServlet")
public class DownLoadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("get");
        String filename = "jingdong-icons.png";
        // 设置响应头如下
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("哈哈哈", "utf-8"));
        //  读取文件   将文件输出到response
        InputStream resource = getServletContext().getResourceAsStream("/static/"+filename);
        ServletOutputStream outputStream = response.getOutputStream();
        byte[] buff = new byte[1024];
        int len = -1;
        while ((len = resource.read(buff)) > -1){
            outputStream.write(buff, 0, len);
        }
        resource.close();
        outputStream.flush();
        outputStream.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
